import asyncio
from utils.db_manager import DBManager

async def update_database():
    """更新数据库内容"""
    try:
        db_manager = DBManager()
        conn = db_manager.get_connection()
        cursor = conn.cursor()
        
        # 1. 更新翻译模型配置
        cursor.execute("""
            INSERT IGNORE INTO models 
            (model_name, model_type, source_lang, target_lang, provider, model_path, config)
            VALUES
            ('Helsinki-NLP/opus-mt-en-zh', 'translation', 'en', 'zh', 'huggingface', 
             'Helsinki-NLP/opus-mt-en-zh', '{"max_length": 512, "num_beams": 4}'),
            ('Helsinki-NLP/opus-mt-zh-en', 'translation', 'zh', 'en', 'huggingface',
             'Helsinki-NLP/opus-mt-zh-en', '{"max_length": 512, "num_beams": 4}'),
            ('facebook/nllb-200-distilled-600M', 'translation', null, null, 'huggingface',
             'facebook/nllb-200-distilled-600M', '{"max_length": 512, "num_beams": 5}')
        """)
        
        # 2. 更新技术术语
        # 2.1 机器学习领域
        cursor.execute("""
            INSERT IGNORE INTO technical_terms 
            (source_term, target_term, source_lang, target_lang, category)
            VALUES
            -- 机器学习领域 (英译中)
            ('neural network', '神经网络', 'en', 'zh', 'machine_learning'),
            ('neural networks', '神经网络', 'en', 'zh', 'machine_learning'),
            ('deep learning', '深度学习', 'en', 'zh', 'machine_learning'),
            ('machine learning', '机器学习', 'en', 'zh', 'machine_learning'),
            ('artificial intelligence', '人工智能', 'en', 'zh', 'machine_learning'),
            ('ai', '人工智能', 'en', 'zh', 'machine_learning'),
            ('backpropagation', '反向传播', 'en', 'zh', 'machine_learning'),
            ('back propagation', '反向传播', 'en', 'zh', 'machine_learning'),
            ('back-propagation', '反向传播', 'en', 'zh', 'machine_learning'),
            ('weights', '权重', 'en', 'zh', 'machine_learning'),
            ('weight', '权重', 'en', 'zh', 'machine_learning'),
            ('training', '训练', 'en', 'zh', 'machine_learning'),
            ('train', '训练', 'en', 'zh', 'machine_learning'),
            ('inference', '推理', 'en', 'zh', 'machine_learning'),
            ('infer', '推理', 'en', 'zh', 'machine_learning'),
            ('optimization', '优化', 'en', 'zh', 'machine_learning'),
            ('optimize', '优化', 'en', 'zh', 'machine_learning'),
            ('gradient descent', '梯度下降', 'en', 'zh', 'machine_learning'),
            ('model', '模型', 'en', 'zh', 'machine_learning'),
            ('models', '模型', 'en', 'zh', 'machine_learning'),
            ('extensive', '大量', 'en', 'zh', 'machine_learning'),
            ('require', '需要', 'en', 'zh', 'machine_learning'),
            
            -- 机器学习领域 (中译英)
            ('神经网络', 'neural network', 'zh', 'en', 'machine_learning'),
            ('深度学习', 'deep learning', 'zh', 'en', 'machine_learning'),
            ('机器学习', 'machine learning', 'zh', 'en', 'machine_learning'),
            ('人工智能', 'artificial intelligence', 'zh', 'en', 'machine_learning'),
            ('反向传播', 'backpropagation', 'zh', 'en', 'machine_learning'),
            ('权重', 'weight', 'zh', 'en', 'machine_learning'),
            ('训练', 'training', 'zh', 'en', 'machine_learning'),
            ('推理', 'inference', 'zh', 'en', 'machine_learning'),
            ('优化', 'optimization', 'zh', 'en', 'machine_learning'),
            ('梯度下降', 'gradient descent', 'zh', 'en', 'machine_learning'),
            ('模型', 'model', 'zh', 'en', 'machine_learning'),
            ('大量', 'extensive', 'zh', 'en', 'machine_learning'),
            ('需要', 'require', 'zh', 'en', 'machine_learning')
        """)
        
        # 2.2 编程领域
        cursor.execute("""
            INSERT IGNORE INTO technical_terms 
            (source_term, target_term, source_lang, target_lang, category)
            VALUES
            -- 编程领域 (英译中)
            ('programming', '编程', 'en', 'zh', 'programming'),
            ('code', '代码', 'en', 'zh', 'programming'),
            ('function', '函数', 'en', 'zh', 'programming'),
            ('class', '类', 'en', 'zh', 'programming'),
            ('object', '对象', 'en', 'zh', 'programming'),
            ('variable', '变量', 'en', 'zh', 'programming'),
            ('loop', '循环', 'en', 'zh', 'programming'),
            ('condition', '条件', 'en', 'zh', 'programming'),
            ('database', '数据库', 'en', 'zh', 'programming'),
            ('algorithm', '算法', 'en', 'zh', 'programming'),
            
            -- 编程领域 (中译英)
            ('编程', 'programming', 'zh', 'en', 'programming'),
            ('代码', 'code', 'zh', 'en', 'programming'),
            ('函数', 'function', 'zh', 'en', 'programming'),
            ('类', 'class', 'zh', 'en', 'programming'),
            ('对象', 'object', 'zh', 'en', 'programming'),
            ('变量', 'variable', 'zh', 'en', 'programming'),
            ('循环', 'loop', 'zh', 'en', 'programming'),
            ('条件', 'condition', 'zh', 'en', 'programming'),
            ('数据库', 'database', 'zh', 'en', 'programming'),
            ('算法', 'algorithm', 'zh', 'en', 'programming')
        """)
        
        conn.commit()
        cursor.close()
        conn.close()
        
        print("Database updated successfully!")
        
        # 清除缓存以确保加载新数据
        db_manager.clear_cache()
        
    except Exception as e:
        print(f"Error updating database: {str(e)}")

if __name__ == "__main__":
    asyncio.run(update_database()) 